package com.deer.wms.base.system.service.zh.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.deer.wms.base.system.dao.ware.ShelfInfoMapper;
import com.deer.wms.base.system.dao.zh.QualityMapper;
import com.deer.wms.base.system.dao.zh.ShelfPlanMapper;
import com.deer.wms.base.system.model.zh.*;
import com.deer.wms.base.system.service.zh.ZhMesService;
import com.deer.wms.base.system.util.HttpRequest;
import com.deer.wms.common.core.service.AbstractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * MWS 对接MES的全部数据接口
 * @author 李帅辰
 * @date 2021/6/10 15:25
 */
@Slf4j
@Service
public class ZhMesServiceImpl extends AbstractService<ShelfPlan,Integer> implements ZhMesService {
    @Autowired
    private ShelfPlanMapper shelfPlanMapper;

    @Autowired
    private ShelfInfoMapper shelfInfoMapper;

    @Autowired
    private QualityMapper qualityMapper;

    @Value("${MES_PATH}")
    private String MES_PATH;
   // public static String MES_PATH="http://10.182.211.15:8083/api/wms/wmsrequest";
//    public static String MES_PATH="http://192.168.1.83:8000/test/test1";


    //同步库存信息
    @Override
    public String SysnBoxItem(ZhBox zhBox) {
        String result_mes="";
        SimpleDateFormat df_12=new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String json = JSONArray.toJSONString(zhBox);
        ZhParam zhParam = new ZhParam();
        zhParam.setJsonString(json);
        zhParam.setUserId("WMS");
        zhParam.setOp("NMMaterialInfo");
        zhParam.setTId("WMS_"+df_12.format(date));
        String str = JSONArray.toJSONString(zhParam);
        result_mes = HttpRequest.sendPost(MES_PATH,str);//路径
        if (null==result_mes||("").equals(result_mes)){
            result_mes = "MES无返回结果集";
        }
        return result_mes;
    }

    //缓存线
    @Override
    public String CacheLineMes(CacheLine cacheLine) {
        String result_mes="";
        SimpleDateFormat df_12=new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        cacheLine.setUserId("MWS");
        String json = JSONArray.toJSONString(cacheLine);
        ZhParam zhParam = new ZhParam();
        zhParam.setJsonString(json);
        zhParam.setUserId("WMS");
        zhParam.setOp("NMStockStateSync");
        zhParam.setTId("WMS_"+df_12.format(date));
        result_mes = HttpRequest.sendPost(MES_PATH, JSONArray.toJSONString(zhParam));//路径
       // List<ZhResult>  zhResults =JSONObject.parseArray(result_mes, ZhResult.class);
      /*  for (ZhResult zhResult : zhResults) {
            String msg = zhResult.getMsg();
            String date = zhResult.getDate();
            Integer code = zhResult.getCode();
            ShelfPlan shelfPlan = new ShelfPlan();
            shelfPlan.setMesResult("响应结果："+msg+",响应时间"+date);
            ShelfInfo shelfInfoByshelfCode = shelfInfoMapper.findShelfInfoByshelfCode(cacheLine.getStockId());
            shelfPlan.setShelfId(shelfInfoByshelfCode.getShelfId());
            shelfPlanMapper.updateShelfPlan(shelfPlan);
        }*/
        if (null==result_mes||("").equals(result_mes)){
            result_mes = "MES无返回结果集";
        }
        return result_mes;
    }

    //同步质检数据
    @Override
    public String QualityMes(Quality quality) {
        String result_mes="";
        SimpleDateFormat df_12=new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();

        Quality result = new Quality();
        result.setMaterialId(quality.getCode());
        if (quality.getResult()==0){
            result.setInspectResult(0);//0，检测成功，1：检测失败
        }else {
            result.setInspectResult(1);
        }
        String json = JSONArray.toJSONString(result);
        ZhParam zhParam = new ZhParam();
        zhParam.setJsonString(json);
        zhParam.setUserId("WMS");
        zhParam.setOp("NMMaterialInspectData");
        zhParam.setTId("WMS_"+df_12.format(date));
        String res = JSONArray.toJSONString(zhParam);
        result_mes = HttpRequest.sendPost(MES_PATH, res);//路径
        //List<ZhResult>  zhResults =JSONObject.parseArray(result_mes, ZhResult.class);
        /*for (ZhResult zhResult : zhResults) {
            List<ZhItem> info = zhResult.getInfo();
            String date = zhResult.getDate();//MES反馈时间
            for (ZhItem zhItem : info) {
                String materialId = zhItem.getMaterialId();
                String msg = zhItem.getMsg();
                quality.setMesResult("MES返回值:"+msg+",反馈时间："+date);
                qualityMapper.updateQuality(quality);
                break;
            }
        }*/
        if (null==result_mes||("").equals(result_mes)){
            result_mes = "MES无返回结果集";
        }
        return result_mes;
    }

    //晶棒入缓存线，发送位置信息给MES
    @Override
    public String CacheLineLocationMes(CacheLine cacheLine) {
        String result_mes="";
            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
            Date date2 = new Date();

            SimpleDateFormat df_12=new SimpleDateFormat("yyMMddHHmmss");
//            List<CacheLine> cacheLines = new ArrayList<>();
            Date date1=new Date();

            cacheLine.setStockInTime(df_12.format(date1).toString());
//            cacheLines.add(cacheLine);
            String json = JSONArray.toJSONString(cacheLine);
            log.info("晶棒入缓存线，发送位置信息给MES的JSON格式:"+json);
            ZhParam zhParam = new ZhParam();
            zhParam.setJsonString(json);
            zhParam.setUserId("WMS");
            zhParam.setOp("NMStockInDJK");
            zhParam.setTId("WMS_"+dateFormat.format(date2));
            String res = JSONArray.toJSONString(zhParam);
            result_mes = HttpRequest.sendPost(MES_PATH, res);//路径
           /* List<ZhResult>  zhResults =JSONObject.parseArray(result_mes, ZhResult.class);
            for (ZhResult zhResult : zhResults) {
                String msg = zhResult.getMsg();
                String date = zhResult.getDate();
            }*/
            if (null==result_mes||("").equals(result_mes)){
                result_mes = "MES无返回结果集";
            }


        return result_mes;
    }

    @Override
    public String BillInUpdateBoxMes(BillInUpdateBox billInUpdateBox){
        String result_mes="";
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        Date date2 = new Date();
        billInUpdateBox.setUserId("WMS");
        String json = JSONArray.toJSONString(billInUpdateBox);
        log.info("晶棒缓存线同步库存信息给MES的JOSO数据:"+json);
        ZhParam zhParam = new ZhParam();
        zhParam.setJsonString(json);
        zhParam.setUserId("WMS");
        zhParam.setOp("NMInStockInfo");
        zhParam.setTId("WMS_"+dateFormat.format(date2));
        String res = JSONArray.toJSONString(zhParam);
        result_mes = HttpRequest.sendPost(MES_PATH, res);//路径
       // List<ZhResult>  zhResults =JSONObject.parseArray(s, ZhResult.class);
        if (null==result_mes||("").equals(result_mes)){
            result_mes = "MES无返回结果集";
        }

        return result_mes;
    }

    @Override
    public String CancelAccount(CacheLine cacheLine) {
        String result_mes="";
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        Date date2 = new Date();
        String json = JSONArray.toJSONString(cacheLine);
        ZhParam zhParam = new ZhParam();
        zhParam.setJsonString(json);
        zhParam.setUserId("WMS");
        zhParam.setOp("NMStockOutDJK");
        zhParam.setTId("WMS_"+dateFormat.format(date2));
        String res = JSONArray.toJSONString(zhParam);
        result_mes = HttpRequest.sendPost(MES_PATH, res);//路径
        if (null==result_mes||("").equals(result_mes)){
            result_mes = "MES无返回结果集";
        }
        cacheLine.setWmsJson(json);
        return result_mes;
    }

    @Override
    public ZhResultMESFinishProduct SelectFinishProduct(String carrierId) {
        String result_mes="";
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        Date date2 = new Date();
        JSONObject jObj = new JSONObject();
        jObj.put("carrierId",carrierId);
        ZhParam zhParam = new ZhParam();
        zhParam.setJsonString(JSONObject.toJSONString(jObj));
        zhParam.setUserId("WMS");
        zhParam.setOp("NMCarrierInfo");
        zhParam.setTId("WMS_"+dateFormat.format(date2));
        String res = JSONArray.toJSONString(zhParam);
        result_mes = HttpRequest.sendPost(MES_PATH, res);//路径
        if (null==result_mes||("").equals(result_mes)){
            result_mes = "MES无返回结果集";
        }
        ZhResultMESFinishProduct result = JSONObject.parseObject(result_mes, ZhResultMESFinishProduct.class);
        return result;
    }

    @Override
    public String NMStockInCPK(ZhParamSyncMESFinishProductInStorage param) {

        String result_mes="";
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        Date date2 = new Date();
        String json = JSONArray.toJSONString(param);

        ZhParam zhParam = new ZhParam();
        zhParam.setJsonString(json);
        zhParam.setUserId("WMS");
        zhParam.setOp("NMStockInCPK");
        zhParam.setTId("WMS_"+dateFormat.format(date2));

        String res = JSONArray.toJSONString(zhParam);
        result_mes = HttpRequest.sendPost(MES_PATH, res);//路径

        if (null==result_mes||("").equals(result_mes)){
            result_mes = "MES无返回结果集";
        }
        return result_mes;
    }

    @Override
    public String NMRunDevice(ZhParamMESRunDevice param) {
        String result_mes="";
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        Date date2 = new Date();
        String json = JSONArray.toJSONString(param);

        ZhParam zhParam = new ZhParam();
        zhParam.setJsonString(json);
        zhParam.setUserId("WMS");
        zhParam.setOp("NMRunDevice");
        zhParam.setTId("WMS_"+dateFormat.format(date2));

        String res = JSONArray.toJSONString(zhParam);
        result_mes = HttpRequest.sendPost(MES_PATH, res);//路径

        if (null==result_mes||("").equals(result_mes)){
            result_mes = "MES无返回结果集";
        }
        return result_mes;
    }

    @Override
    public String NMChangeNG(ShelfPlan shelfPlan) {
        String result_mes="";
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        Date date2 = new Date();
        String json = JSONArray.toJSONString(shelfPlan);
        ZhParam zhParam = new ZhParam();//给Mes传递的参数
        zhParam.setJsonString(json);
        zhParam.setUserId("WMS");
        zhParam.setOp("NMChangeNG");
        zhParam.setTId("WMS_"+dateFormat.format(date2));
        String str = JSONArray.toJSONString(zhParam);
        result_mes = HttpRequest.sendPost(MES_PATH,str);//路径
        if (null==result_mes||("").equals(result_mes)){
            result_mes = "MES无返回结果集";
        }
        return result_mes;
    }

    @Override
    public ZhResult NMChangeNGMEsToWms(ZhParam zhParam) {
        String jsonString = zhParam.getJsonString();
        ShelfPlan shelfPlan = JSONArray.parseObject(jsonString, ShelfPlan.class);//获取到任务
        String materialId = shelfPlan.getMaterialId();
        ZhResult zhResult = new ZhResult();
        zhResult.setId(materialId);
        zhResult.setCode(0);
        zhResult.setMsg("接受成功");
        SimpleDateFormat df_12=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1=new Date();
        zhResult.setDate(df_12.format(date1));
        return zhResult;
    }
}
